<%-- 
    Document   : dofindtrip
    Created on : Dec 18, 2012, 2:10:25 PM
    Author     : alexxxey
--%>

<%@page import="java.text.ParseException"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.DateFormat"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Booking Tickets Web Appliaction</title>
    </head>
    <body>
        <%@page import="javax.naming.*, bookingtickets.*" %>        
        <%!
            BookingTicketsSessionRemote ejbRef = null;               
        %>
        <%  
            List trips = null;           
            SimpleDateFormat df;
            SimpleDateFormat tf;
            boolean inputDataValid = true;
            
            InitialContext ic = new InitialContext();
            ejbRef = (BookingTicketsSessionRemote)ic.lookup("bookingtickets.BookingTicketsSessionRemote");                        
            df = new SimpleDateFormat("dd.MM.yyyy");
            tf = new SimpleDateFormat("HH:mm:ss");
            
            if (request.getParameter("findType").equals("all"))
            {                
                trips = null;            
                inputDataValid = true;
                trips = ejbRef.getAllTrips();
            }
            else if (request.getParameter("findType").equals("date"))
            {
                String dateStr = new String(request.getParameter("tripDay").getBytes("ISO-8859-1"));                
                SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy");                
                try
                {
                    Date d = f.parse(dateStr);
                    inputDataValid = true;
                    trips = null;
                    trips = ejbRef.findTripsForDate(d);                
                }
                catch (ParseException e)
                {
                    inputDataValid = false;
                }
                
            }
            else if (request.getParameter("findType").equals("destination"))
            {
                trips = null;      
                inputDataValid = true;                 
                String dest = new String(request.getParameter("tripDest").getBytes("ISO-8859-1"));                   
                trips = ejbRef.findTripsForDestinations(dest);                
            }                                    
        %>
                 
        <%-- Таблица с результатами поиска --%>
        <h3>Результаты поиска:</h3>          
        <%
        if (!inputDataValid)
        {%>
            <h4>Данные для поиска введены некорректно!</h4>
        <% }
        else if (trips != null && trips.size()!=0)
        {%>

        <form action="doSetTicketParam.jsp" method="POST"> 
            <table BORDER=2>

                <tr>
                    <td align=center> </td>
                    <td align=center>Номер</td>                
                    <td align=center>Отправление</td>
                    <td align=center>Число мест</td>
                    <td align=center>Свободные места</td>                                    
                    <td>Пункты назначения</td> 
                </tr>

                <% for (int row = 0; row < trips.size(); row++)
                    {%>        
                        <%
                            Trip trip = (Trip)trips.get(row);
                            String tripId = new Integer(trip.getId()).toString();                        
                            String date = df.format(trip.getTripdate()) + " " + tf.format(trip.getTriptime());                           
                            String placesNum = new Integer(trip.getPlacesnum()).toString(); 
                            String freePlaces = new Integer(trip.getFreePlacesNum()).toString(); 

                            List destList = trip.getDestinationList();
                            String destStr = new String();
                            for (int i=0; i<destList.size(); ++i)
                            {
                                Destination d = (Destination)destList.get(i);
                                destStr += d.getDest();
                                if (i!=destList.size()-1)
                                    destStr += ", ";
                            }                                                    
                        %>
                    <tr>    
                        <%  
                            if (trip.getFreePlacesNum() != 0)
                            {
                                if (row == 0) 
                                { %>
                                <td> <input type="radio" name="chooseTrip" checked
                                            value=<%=(new Integer(trip.getId()).toString())%>> </td>
                            <%  } 
                                else 
                                { %>
                                    <td> <input type="radio" name="chooseTrip" 
                                                value=<%=(new Integer(trip.getId()).toString())%>> </td>
                             <% } 
                            }
                            else 
                            {
                                %><td align=center> </td><%
                            }
                        %> 
                        <td align=center> <%=(tripId)%> </td>
                        <td align=center> <%=(date)%> </td>
                        <td align=center> <%=(placesNum)%> </td>  
                        <td align=center> <%=(freePlaces)%> </td>                    
                        <td > <%=(" " + destStr)%> </td>                    
                    </tr>

                <% } 
                        trips = null; %>
            </table> 

            <input type="submit" value="Резервирование билетов" />
        </form>
        <% 
        }
        else
        { %>
            <h4>Ничего не найдено</h4> 
        <% 
        } 
        %>                                              
        <p><a href="findTrip.jsp">Поиск маршрутов</a></p>
        <p><a href="index.jsp">Главная страница</a></p>
    </body>
</html>
